package cn.com.huzo.service.oa.impl;

import cn.com.huzo.model.entities.oa.ToDoEntity;
import cn.com.huzo.repository.oa.ToDoRepository;
import cn.com.huzo.service.oa.ToDoManager;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.activiti.engine.TaskService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 待办事项管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("ToDoManager")
public class ToDoManagerImpl extends GenericManagerImpl<ToDoEntity, String>
        implements ToDoManager {

    @Resource(name = "ToDoRepository")
    private ToDoRepository _dao_todo;

    @Autowired
    private TaskService taskService;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer fetchForCount(Map<String, Object> criterion) throws Exception {
        // 任务候选人  
        String candidate = "部门经理";
        return _dao_todo.fetchForCount(criterion);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchTaskForPage(Map<String, Object> criterion) throws Exception {
        // 任务候选人  
        String candidate = "部门经理";
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao_todo.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<ToDoEntity> toDoList = _dao_todo.fetchForPage(criterion);
            Paginator<ToDoEntity> paginator = new Paginator<>(toDoList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> assigneeTask(Long userId, String taskId) throws Exception {
        Map<String, Object> _result = new HashMap();

        taskService.setOwner(taskId, userId.toString());    // 设置任务的所有人
        taskService.claim(taskId, userId.toString());       // 签收,这个时候就任务Task的Assignee属性就有了值

        _result.put("Success", true);
        return _result;
    }

    /**
     * 驳回流程
     *
     * @param taskId 当前任务ID
     * @param activityId 驳回节点ID
     * @param variables 流程存储参数
     * @throws Exception
     */
    public void backProcess(String taskId, String activityId, Map<String, Object> variables) throws Exception {
        if (StringUtils.isEmpty(activityId)) {
            throw new Exception("驳回目标节点ID为空！");
        }

        // 查询本节点发起的会签任务，并结束
        // 查找所有并行任务节点，同时驳回
    }
}
